﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Distribution.Controllers
{
    public class SkuController : GeneralController {
        private ISkuInfoService _iSkuInfoService;
        private IMerchantService _iMerchantService;
        public SkuController(ISkuInfoService iSkuInfoService, IMerchantService iMerchantService) {
            _iSkuInfoService = iSkuInfoService;
            _iMerchantService = iMerchantService;
        }


        [HttpGet]
        public JsonResult Gets() {
            var result = _iSkuInfoService.Gets(currentUser.user_id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Get(long id) {
            var result = _iSkuInfoService.Get(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [ValidateInput(false)]
        public JsonResult Save(sku_info_model model) {
            model.bind_no = _iMerchantService.Get(currentUser.user_id).merchant_no;
            ApiResultMessage result;
            try {
                result = _iSkuInfoService.Save(model);
            } catch (Exception ex) {
                result =new ApiResultMessage(500,ex.Message);
                return Json(ResultToJson.Json(result.result_code, result.result_msg,""), JsonRequestBehavior.AllowGet);
            }
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [HttpDelete]
        public JsonResult Delete(long id) {
            var result = _iSkuInfoService.DeleteSku(id);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }
    }
}